/*******************************************************************************
 * CAROL CHOU
 * RA8806 / 240*160 / Winbond
 ********************************************************************************/



/*#include "Demo.h"
#include "stdlib.h"
#include <intrins.h>
#include <absacc.h>

#include "homepage_240160.h"
#include "RAiO_240160.h"
#include "TP_Button_pic.h"
//#include "ic.h"
#include "Demo_Function_Page.h"
#include "Product_Introduce.h"
#include "function_select.h"
//#include "train_160x70.h"
#include "scroll.h"
#include "sleep_mode.h"*/
#include "userdef.h"
#include "RA8806Reg.h"
#include "TopHeadline_24060.h"
#include "front_page_ic.h"
#include "RA8806.h"
#define CONFIG_PLATFORM Nuvoton_EVB
//#define PRETEST
#define Keyboard_Emu

extern void Init_console(void);
extern void Init_Interface_IO(void);
extern void LCD_Reset(void);
extern void LCD_Initial(void);
extern void LCD_CmdWrite(uchar);
extern void LCD_DataWrite(uchar);
/*extern uchar LCD_CmdRead(void);
extern uchar LCD_DataRead(void);*/
extern void LCD_ON(void);
extern void LCD_Clear(void);
extern void Access_Page1(void);
extern void Only_Show_Page1(void);
extern void COM_DIR_239_0(void);
extern void SEG_DIR_319_0(void);
extern void LCD_Graphic(void);
extern void LCD_GotoXY(uchar x, uchar y);
extern void LCD_Text(void);
extern void LCD_ShowStr(unsigned char *text, unsigned int t);
extern void LCD_Display_Object(const struct STN_data *pObj_info, int inverse);



struct STN_img_data
{
  char *content;
  int disp_x, disp_y;
  int width, height;
  int layer; //0:layer1, 1:layer2,   two layer
};

char buf2[100];
struct Rect ROI;
//char layer1_DRAM1[240*160/8], layer2_DRAM2[240*160/8] ;
//char layer2_DRAM2[6630];
struct STN_data temp_disp_data = { buf2, 0, 0, 0, 0, 0 };
struct STN_data disp_data[] = 
{
  "Sample Selection", 0, 0, 0, 0, 0,
  "=", 30, 0, 16, 0, 0,
  "1.  dsDNA", 0, 0, 32, 0, 0,
  "2.  ssDNA", 0, 0, 48, 0, 0,
  "3.  RNA", 0, 0, 64, 0, 0,
  "4.  Protein", 0, 0, 80, 0, 0,
//  "*   Main menu", 0, 0, 96, 0, 0,
//  "Select:          Enter:   ", 0, 128, 0,
  "=", 30, 0, 112, 0, 0,
  buf2, 0, 0, 0, 0, 0
};

struct STN_data MM_disp_data[] = 
{
  "Main Menu", 0, 0, 0, 0, 0,
  "=", 30, 0, 16, 0, 0,
  "1.  Sample Selection", 0, 0, 32, 0, 0,
  "2.  Starting Measure", 0, 0, 48, 0, 0,
  "3.  Show & Print Result", 0, 0, 64, 0, 0,
  "4.  About MaestroNano", 0, 0, 80, 0, 0,
  "=", 30, 0, 112, 0, 0
};

struct STN_data Measure_disp_data[] = 
{
  "=", 30, 0, 0, 0, 0,
  "MEA. blank", 0, 0, 16, 0, 0,
  "MEA. samples", 0, 0, 32, 0, 0,
  "Back to menu", 0, 0, 48, 0, 0,
  "=", 15, 0, 64, 0, 0,
  "** blank", 0, 0, 80, 0, 0,
  "** sample_", 0, 0, 96, 0, 0,
  "** sample_", 0, 0, 112, 0, 0,
  "** sample_", 0, 0, 128, 0, 0,
  "** sample_", 0, 0, 144, 0, 0,
};

struct STN_data Sheet_disp_data[] = {
  "=", 30, 0, 0, 0, 0,
  "Sheet#  Date     Time Sample", 0, 0, 16, 0, 0,
  "=", 30, 0, 32, 0, 0,
  "*** Back to main menu", 0, 0, 48, 0, 0
};

struct STN_data Sheet_Item_disp_data[] = {
  "=", 30, 0, 0, 0, 0,
  "Prn. All", 0, 0, 16, 0, 0,
  "Prn. Select", 0, 0, 32, 0, 0,
  "Sheets menu", 0, 0, 48, 0, 0,
  "=", 15, 0, 64, 0, 0
};

struct STN_data Measure_disp_msg[] = 
{
  "Do you want to back to main menu?", 0, 0, 0, 0, 0,
  "==============================", 0, 0, 32, 0, 0,
  "*** Save & back to main menu", 0, 0, 48, 0, 0,
  "*** Back to main menu", 0, 0, 64, 0, 0,
  "*** Cancel", 0, 0, 80, 0, 0,
};

struct STN_data icon_data [] = {
  (char*)gImage_dsDNA, 80 << 16 | (sizeof(gImage_dsDNA)*8)/80, 16, 32, 0, 1,
  (char*)gImage_ssDNA, 80 << 16 | (sizeof(gImage_ssDNA)*8)/80, 16, 32, 0, 1,
  (char*)gImage_RNA, 80 << 16 | (sizeof(gImage_RNA)*8)/80, 16, 32, 0, 1,
  (char*)gImage_protein, 80 << 16 | (sizeof(gImage_protein)*8)/80, 16, 32, 0, 1,
  (char*)gImage_up_key, 32 << 16 | (sizeof(gImage_up_key)*8)/32, 4, 128, 0, 1,
  (char*)gImage_down_key, 32 << 16 | (sizeof(gImage_down_key)*8)/32, 12, 128, 0, 1,
  (char*)gImage_enter_key, 32 << 16 | (sizeof(gImage_enter_key)*8)/32, 20, 128, 0, 1,
  (char*)gImage_yes, 40 << 16 | (sizeof(gImage_yes)*8)/40, 6, 64, 0, 1,
  (char*)gImage_no, 40 << 16 | (sizeof(gImage_no)*8)/40, 18, 64, 0, 1
};

struct STN_data  logo_disp_data[] = {
	(char*)gImage_logo3, 240 << 16 | (sizeof(gImage_logo3)*8)/240, 0, 20, 0, 1,
  "http://www.maestrogen.com/", 0, 0, (((20 + sizeof(gImage_logo3))*8/240 /  16 + 2) * 16), 0, 0
};

struct STN_data page4_disp_data[] = 
{
  (char*)gImage_logo_sample, -1, 4, 16, 2, 0, (char*)gImage_logo_blank,- 1, 19, 16, 2, 0, 
  (char*)gImage_logo_mode, -1, 4, 96, 2, 0, (char*)gImage_logo_list, -1, 19, 96, 2, 0
};
int mode_index = -1, menu_index = -1, measure_index = -1, head_sheet = -1, tail_sheet = -1;
struct Measure_tran temp_mea1;
union MaestroDate curdate;
int cur_sheet = 0, Select_sheet = -1;
struct Measure_sheet sheet_info[MAX_Sheets];

//Page3: measuring
void Display_page3()
{
  int i = 0;
  LCD_Set(0xff, 2);
  LCD_Set(0x00, 1);
  //	Only_Show_Page2();
  LCD_Text();

  Access_Page1();
  LCD_GotoXY(0, 64);
  LCD_ShowStr("Measuring xxxxx", 5);
  //    XOR_Two_Layer();
  //    Show_Two_Layer();

  strcpy(buf2, "...");
  while (i < 6)
  {
    LCD_GotoXY(0, 80);
    LCD_ShowStr(buf2, 5);
    printf("%s\n", buf2);
    strcat(buf2, "...");
    Delay100ms(10);
    i++;
  }
}

// show and print the selected sheet

void Show_and_Print()
{
  int i, j, selected_index[MAX_Samples+1], upper_index = 3, lower_index = 1, Region_index = 1, active_length, lower_index1 = -1, upper_index1 = -1, key;
  sheet_info[Select_sheet-1];

  screen_inverse 
  LCD_Set(0xff, 2);
  LCD_Set(0x00, 1);
  LCD_Text();

  for (i = 0; i < sizeof(selected_index)/sizeof(int); i++)
	 selected_index[i] = -1;

  for (i = 0; i < 5; i++)
     LCD_Display_Object(&Sheet_Item_disp_data[i], 0);
  LCD_Text();
  for (i = 0; i < STN_Height; i+=16) {
     LCD_GotoXY(15, i);
	 LCD_ShowStr("|", 0);
  }

  strcpy(buf2, "function");
  Show_text1((15 - strlen(buf2))/2, 0, buf2)
  sprintf(buf2, "Sheet_%d", Select_sheet);
  //strcpy(buf2, "Item");
  Show_text1((15 - strlen(buf2))/2, 64, buf2)
  strcpy(buf2, "Desc.");
  Show_text1(16+(15 - strlen(buf2))/2, 0, buf2)
  active_length = strlen(Sheet_Item_disp_data[Region_index].content);
  LCD_Set_Rect(Sheet_Item_disp_data[Region_index].disp_x, Sheet_Item_disp_data[Region_index].disp_y, 8*(active_length), 16, 0x00, 2);
  j = Region_Show_text("Press", 16, 29, 16, 80);
  Show_img(&temp_disp_data, (char*)gImage_enter_key, 32 << 16 | (sizeof(gImage_enter_key)*8)/32, 23, 16, 0, 1, 1);
  j = Region_Show_text("to print all measure result!", 16, 29, 48, 96);
  Show_img(&temp_disp_data, (char*)gImage_printer, 48 << 16 | (sizeof(gImage_printer)*8)/48, 18, 112, 0, 1, 1);
  j = temp_mea1.sample_index+temp_mea1.blank_flag;

  if (j > 0) {   //this sheet exist item
	for (i = 0; i <= j && i <= 4; i++) {
	   if (i==0) {
	     LCD_Display_Object(&Measure_disp_data[5], 0);
		 upper_index += 2;
		 lower_index1 = upper_index1 = 0;
	   }
	   else {
	      strcpy(buf2, Measure_disp_data[5+i]);
	      sprintf(buf2, "%s%d", Measure_disp_data[5+i].content, i);
		  Show_text1(Measure_disp_data[5+i].disp_x, Measure_disp_data[5+i].disp_y, buf2);
		  upper_index++;
		  upper_index1++;
	   }
	}
	if (j > 4) {
	  buf2[1] = 0x00; buf2[0] = 0x19;
	  //buf2[1] = 0x00; buf2[0] = 0xfb;
	  Show_text(14, 144, buf2);
	}
  }

  while (1) {
    key = getchar();
	if (key==Up_key || key==Down_key || key==Enter_key) {
	  if (1 <= Region_index && Region_index <= 3) 
	    LCD_Set_Rect(Sheet_Item_disp_data[Region_index].disp_x, Sheet_Item_disp_data[Region_index].disp_y, 8*(14), 16, 0xff, 2);
	  else
	     LCD_Set_Rect(Measure_disp_data[Region_index].disp_x, Measure_disp_data[Region_index].disp_y, 8*(14), 16, 0xff, 2);
	}
	else
	   continue;

	if (key==Up_key) {
	  Region_index--;
	  if (Region_index < lower_index) {
	    Region_index = lower_index;
	  }
	  else
         if (Region_index==4) {
           if (lower_index1 > 0) {
			 lower_index1--; upper_index1--;Region_index++;
			 for (i = 5, j = lower_index1; i < 10; i++, j++) {
				if (j==0) {
				  buf2[1] = 0x00; buf2[0] = 0x20;
			      Show_text1(14, 80, buf2);
			      sprintf(buf2, "%s", Measure_disp_data[5].content);
				}
			    else
                   sprintf(buf2, "%s%d", Measure_disp_data[9].content, j);

				Show_text1(Measure_disp_data[i].disp_x, Measure_disp_data[i].disp_y, "             ");
				if (i==5) {
				    Show_text(Measure_disp_data[i].disp_x, Measure_disp_data[i].disp_y, buf2);	
				}
				else {
			       Show_text1(Measure_disp_data[i].disp_x, Measure_disp_data[i].disp_y, buf2);
				}
				if (selected_index[j]==1) {
		          buf2[1] = 0x00; buf2[0] = 0xfb;
				}
				else {
				   buf2[1] = 0x00; buf2[0] = 0x20;
				}
				Show_text1(13, i*16, buf2)
			 }             
		   }
		   else
		      Region_index--;
		 }
	}
	else
	   if (key==Down_key) {
	     Region_index++;
	     if (Region_index > upper_index) {
		   Region_index = upper_index;
		   if (upper_index==9) {
			 if (upper_index1 < (temp_mea1.sample_index+temp_mea1.blank_flag)) {
			   lower_index1++; upper_index1++;
			   buf2[1] = 0x00; buf2[0] = 0x18;
			   Show_text(14, 80, buf2);
			   for (i = 5, j = lower_index1; i < 10; i++, j++) {
			      sprintf(buf2, "%s%d", Measure_disp_data[9].content, j);
				  Show_text1(Measure_disp_data[i].disp_x, Measure_disp_data[i].disp_y, "             ");
				  if (i==9) {
				    Show_text(Measure_disp_data[i].disp_x, Measure_disp_data[i].disp_y, buf2);	
				  }
				  else {
			         Show_text1(Measure_disp_data[i].disp_x, Measure_disp_data[i].disp_y, buf2);
				  }
				  if (selected_index[j]==1) {
		            buf2[1] = 0x00; buf2[0] = 0xfb;
				  }
				  else {
				     buf2[1] = 0x00; buf2[0] = 0x20;
				  }
				  Show_text1(13, i*16, buf2)
			   }
			 }
		   }
		 }
		 else
			if (Region_index==4)
			  Region_index++; 
	   }

	switch(Region_index) {
      case 1:
	  case 3:
         strcpy(buf2, "==============");
		 Show_text1(16, 0, buf2)
		 strcpy(buf2, "Desc.");
         Show_text1(16+(15 - strlen(buf2))/2, 0, buf2)
	     break;
	  case 5:
         strcpy(buf2, "==============");
		 Show_text1(16, 0, buf2)
		 strcpy(buf2, "Output");
         Show_text1(16+(15 - strlen(buf2))/2, 0, buf2)
		 break;
	}
	if (Region_index <= 3) {
	  active_length = strlen(Sheet_Item_disp_data[Region_index].content);
	  LCD_Set_Rect(Sheet_Item_disp_data[Region_index].disp_x, Sheet_Item_disp_data[Region_index].disp_y, 8*(active_length), 16, 0x00, 2);

	  LCD_Set_Rect(16, 16, 8*(29-16+1), 144, 0x00, 1);
	  j = Region_Show_text("Press", 16, 29, 16, 80);
      Show_img(&temp_disp_data, (char*)gImage_enter_key, 32 << 16 | (sizeof(gImage_enter_key)*8)/32, 23, 16, 0, 1, 1);
	  switch(Region_index) {
	    case 1:
		   j = Region_Show_text("to print all measure result!", 16, 29, 48, 96);
		   Show_img(&temp_disp_data, (char*)gImage_printer, 48 << 16 | (sizeof(gImage_printer)*8)/48, 18, 112, 0, 1, 1);
	       break;
	    case 2:
		   j = Region_Show_text("to print selected measure resul!", 16, 29, 48, 96);
		   Show_img(&temp_disp_data, (char*)gImage_printer, 48 << 16 | (sizeof(gImage_printer)*8)/48, 18, 112, 0, 1, 1);
	       break;
	    case 3:
	       j = Region_Show_text("to back to sheet menu!", 16, 29, 48, 96);
		   Show_img(&temp_disp_data, (char*)gImage_sheet, 40 << 16 | (sizeof(gImage_sheet)*8)/40, 18, 112, 0, 1, 1);
	       break;
	  }
	}
	else {
	    if ((upper_index1 - (9-Region_index)) > 0) {
		   sprintf(buf2, "%s%d", Measure_disp_data[9].content, upper_index1 - (9-Region_index));
		   active_length = strlen(buf2);
           LCD_Set_Rect(Measure_disp_data[Region_index].disp_x, Measure_disp_data[Region_index].disp_y, 8*(active_length), 16, 0x00, 2);
		   LCD_Set_Rect(16, 16, 8*(29-16+1), 144, 0x00, 1);
		   Display_measure_output(&temp_mea1, sheet_info[Select_sheet-1].sheet_type);
	    }
		else
		   if (Region_index==5) {
		     active_length = strlen(Measure_disp_data[Region_index].content);
		     LCD_Set_Rect(Measure_disp_data[Region_index].disp_x, Measure_disp_data[Region_index].disp_y, 8*(active_length), 16, 0x00, 2);
			 LCD_Set_Rect(16, 16, 8*(29-16+1), 144, 0x00, 1);
		   }
		   else {
		      active_length = strlen(Measure_disp_data[Region_index].content)+1;
		      LCD_Set_Rect(Measure_disp_data[Region_index].disp_x, Measure_disp_data[Region_index].disp_y, 8*(active_length), 16, 0x00, 2);
			  LCD_Set_Rect(16, 16, 8*(29-16+1), 144, 0x00, 1);
			  Display_measure_output(&temp_mea1, sheet_info[Select_sheet-1].sheet_type);
		   }
	}

	if (key==Enter_key) {
	  switch(Region_index) {
		case 1:
		   Print_All(&temp_mea1);
		   break;
		case 2:
		   Print_Selected(&temp_mea1, selected_index);
		   break;
		case 3:
		   return;
		   break;
		case 5:
		case 6:
		case 7:
		case 8:
		case 9:
		   if (selected_index[Region_index-5+lower_index1]==-1) {
		     buf2[1] = 0x00; buf2[0] = 0xfb;
		     Show_text1(13, Region_index*16, buf2);
		     selected_index[Region_index-5+lower_index1] = 1;
		   }
		   else {
		      buf2[1] = 0x00; buf2[0] = 0x20;
		      Show_text1(13, Region_index*16, buf2);
		      selected_index[Region_index-5+lower_index1] = -1;		   
		   }
		   break;
	  }
	}
  }
}

//Page6:  show & print result
#define MAX_Page_Sheets 7
void Display_page6()
{
  int i, j, lower_index = -1, upper_index = -1, key, Region_index = -1, lower_index1 = -1, upper_index1 = -1, total_sheets = 0, show_back = 1;
  int circular_index[MAX_Sheets] = { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 };
  printf("sheet circular index %d %d\n", head_sheet, tail_sheet);

show_and_print:
  screen_inverse 
  LCD_Set(0xff, 2);
  LCD_Set(0x00, 1);
  LCD_Text();

  for (i = 0; i < 4; i++)
     LCD_Display_Object(&Sheet_disp_data[i], 0);
  strcpy(buf2, "Measure results");
  Show_text1((30 - strlen(buf2))/2, 0, buf2)

  if (MAX_Sheets > (MAX_Page_Sheets-1) && (tail_sheet < head_sheet) || ((head_sheet < tail_sheet) && tail_sheet > (MAX_Page_Sheets-1))) {  //draw down arrow
	buf2[1] = 0x00; buf2[0] = 0x19;
	Show_text(29, 144, buf2);
  }
  if ((upper_index1-lower_index1)==(MAX_Page_Sheets-1)) {
	buf2[1] = 0x00; buf2[0] = 0x18;
	Show_text(29, 48, buf2);
  }

  total_sheets = 0;
  if (head_sheet < tail_sheet) {
    for (i = 0; i < MAX_Sheets; i++)
	   if (i < tail_sheet) {
	     circular_index[i] = tail_sheet - i;
		 total_sheets++;
		 if (lower_index1 < 0)
		   lower_index1 = i;
       }
	   else {
          circular_index[i] = -1;
          if (upper_index1 < 0) {
		    if (total_sheets < MAX_Page_Sheets)
              upper_index1 = total_sheets - 1;
		    else
               upper_index1 = MAX_Page_Sheets - 1;
          }
       }
  }
  else 
     if (head_sheet > tail_sheet) {
       for (j = 0, i = tail_sheet - 1; i >= 0; i--)
	      circular_index[j++] = i + 1;
       for (i = MAX_Sheets-1; i >= tail_sheet; i--)
          circular_index[j++] = i+1;
	   total_sheets = MAX_Sheets;
       if (lower_index1 < 0 && upper_index1 < 0) {
	     lower_index1 = 0;
         upper_index1 = MAX_Page_Sheets - 1;
       }
     }
  if (show_back==1 && upper_index1==(MAX_Page_Sheets - 1))
    upper_index1--;
  for (i = 0 ; i < MAX_Sheets; i++)
     printf("%d ", circular_index[i]);
  printf("\n");

  lower_index = 3;
  if (lower_index1 >= 0) {
//    lower_index = 3;
    upper_index = 3 + upper_index1 - lower_index1 + show_back;
  }
  else
	  upper_index = lower_index;
  for (i = lower_index1; 0 <= i && i <= upper_index1; i++) {
	 if (circular_index[i]==-1) {
	   break;
	 }
	 else {
	    j = sheet_info[circular_index[i]-1].sheet_type - 1;
		if (circular_index[i] < 10)
		  sprintf(buf2, "Sheet%d  ******** xxxx %s", circular_index[i], disp_data[j+2].content+4);
		else
		   sprintf(buf2, "Sheet%d ******** xxxx %s", circular_index[i], disp_data[j+2].content+4);
		printf("kkk%s\n", buf2);
		Show_text1(0, 16*(3+i-lower_index1+show_back), buf2);

		/*if (lower_index < 0) {
		  lower_index = upper_index = 3;
		}
		else
           upper_index++;*/
	 }   
  }
  
  if (Region_index < 0)
    Region_index = lower_index;
  if (lower_index > 0) {
    if (show_back==1 && Region_index <= 3) {
	  LCD_Set_Rect(Sheet_disp_data[Region_index].disp_x, Sheet_disp_data[Region_index].disp_y, 8*strlen(Sheet_disp_data[Region_index].content), 16, 0x00, 2);
	}
	else
	   LCD_Set_Rect(0, Region_index*16, 239, 16, 0x00, 2);
  }
  while (lower_index > 0) {
    key = getchar();
	if (key==Up_key || key==Down_key || key==Enter_key) {
      if (Region_index==3 || Region_index==9)
	    LCD_Set_Rect(0, Region_index*16, 239, 16, 0xff, 2);
      else
         LCD_Set_Rect(0, Region_index*16, 240, 16, 0xff, 2);
    }
	else
	   continue;

	if (key==Up_key) {
	  Region_index--;
	  if (Region_index < lower_index) {
		if (lower_index1 >= 0) {
		  if (lower_index1 > 0)
		    lower_index1--;
		  else {
		      show_back = 1;
			  Show_text1(Sheet_disp_data[3].disp_x, Sheet_disp_data[3].disp_y, "                             ");
              Show_text1(Sheet_disp_data[3].disp_x, Sheet_disp_data[3].disp_y, Sheet_disp_data[3].content)
		  }
		  if (upper_index1 >= (MAX_Page_Sheets-1))
		    upper_index1--;
		  if (show_back==1) {										//remove up scroll arrow
			buf2[1] = 0x00; buf2[0] = 0x20;
			Show_text1(29, 48, buf2);
		  }
		  for (i = lower_index1; i <= upper_index1; i++) {
	         j = sheet_info[circular_index[i]-1].sheet_type - 1;
		     if (circular_index[i] < 10)
		       sprintf(buf2, "Sheet%d  ******** xxxx %s", circular_index[i], disp_data[j+2].content+4);
		     else
		        sprintf(buf2, "Sheet%d ******** xxxx %s", circular_index[i], disp_data[j+2].content+4);
		        printf("kkk%s\n", buf2);
			 Show_text1(0, 16*(3+i-lower_index1+show_back), "                             ");
		     Show_text1(0, 16*(3+i-lower_index1+show_back), buf2);		  
		  }
		}
		Region_index = lower_index;
	  }
	}
	else
	   if (key==Down_key) {
	     Region_index++;
	     if (Region_index > upper_index) {
	       Region_index = upper_index;
		   if (upper_index==9) {
			 if (upper_index1 < (total_sheets-1)) {						//show up scroll arrow
			   upper_index1++;
			   buf2[1] = 0x00; buf2[0] = 0x18;
			   Show_text(29, 48, buf2);
			   if (show_back==0)
			     lower_index1++;
			   else
                  show_back = 0;
		       for (i = lower_index1; i <= upper_index1; i++) {
	              j = sheet_info[circular_index[i]-1].sheet_type - 1;
		          if (circular_index[i] < 10)
		            sprintf(buf2, "Sheet%d  ******** xxxx %s", circular_index[i], disp_data[j+2].content+4);
		          else
		             sprintf(buf2, "Sheet%d ******** xxxx %s", circular_index[i], disp_data[j+2].content+4);
		          printf("kkk%s\n", buf2);
				  Show_text1(0, 16*(3+i-lower_index1), "                             ");
		          Show_text1(0, 16*(3+i-lower_index1), buf2);		  
		       }
			 }
		   }
         }
       } 
	if (Region_index==3 || Region_index==9) {
	  if (show_back==1 && Region_index==3)
	    LCD_Set_Rect(Sheet_disp_data[Region_index].disp_x, Sheet_disp_data[Region_index].disp_y, 8*strlen(Sheet_disp_data[Region_index].content), 16, 0x00, 2);
	}
	else
       LCD_Set_Rect(0, Region_index*16, 239, 16, 0x00, 2);
    if (key==Enter_key) {
	  if (Region_index==3 && show_back==1)
	    return;
      LCD_Set_Rect(0, 0, 240, 160, 0x00, 1);
	  LCD_Set_Rect(0, 0, 240, 160, 0xff, 2);
      Select_sheet = circular_index[Region_index - 3 + lower_index1-show_back];
	  Show_and_Print();
//	  getchar();
      goto show_and_print;
//      return;
    }
  }
//  strcpy(buf2, "test");
//  Show_text1(0, 48, buf2);
//  LCD_Set_Rect(0, 48, 240, 16, 0x00, 2);
}

//Page5:  measurement page
void Display_page5()
{
  int i, j, Region_index = 1, lower_index = 1, upper_index = 3, key, lower_index1 = -1, upper_index1 = -1, err_no, redraw_screen = 0, active_length, key1, is_save = 0;

//  temp_mea1.tran_index = ;
  temp_mea1.blank_flag = -1;
  temp_mea1.sample_index = -1;
  printf("current active sheet: %d\n", cur_sheet);
/*  temp_mea1.blank.Xenon_I = ;
  temp_mea1.blank.Non_Xenon_I = ;
  temp_mea1.samples[temp_mea1.sample_index].Xenon_I = ;
  temp_mea1.samples[temp_mea1.sample_index].Non_Xenon_I = ;*/
Measure_menu:
  screen_inverse 
  LCD_Set(0xff, 2);
  LCD_Set(0x00, 1);
  LCD_Text();

  for (i = 0; i < 5; i++)
     LCD_Display_Object(&Measure_disp_data[i], 0);

  LCD_Text();
  for (i = 0; i < STN_Height; i+=16) {
     LCD_GotoXY(15, i);
	 LCD_ShowStr("|", 0);
  }

  Show_text1((15 - strlen(disp_data[mode_index+2].content+4))/2, 0, disp_data[mode_index+2].content+4)
//  strcpy(buf2, "Sheet");
  if (cur_sheet==MAX_Sheets)
    cur_sheet = 0;
  sprintf(buf2, "Sheet_%d", cur_sheet+1);
  Show_text1((15 - strlen(buf2))/2, 64, buf2)
  strcpy(buf2, "Desc.");
  Show_text1(16+(15 - strlen(buf2))/2, 0, buf2)
  active_length = strlen(Measure_disp_data[Region_index].content);
  LCD_Set_Rect(Measure_disp_data[Region_index].disp_x, Measure_disp_data[Region_index].disp_y, 8*(active_length), 16, 0x00, 2);
  j = Region_Show_text("Press", 16, 29, 16, 80);
  Show_img(&temp_disp_data, (char*)gImage_enter_key, 32 << 16 | (sizeof(gImage_enter_key)*8)/32, 23, 16, 0, 1, 1);
  j = Region_Show_text("to measure blank!", 16, 29, 48, 80);
  Show_img(&temp_disp_data, (char*)gImage_logo_blank1, 48 << 16 | (sizeof(gImage_logo_blank1)*8)/48, 18, 112, 0, 1, 1);

  while (1) {
	if (redraw_screen==1) {
	  if ((temp_mea1.sample_index+temp_mea1.blank_flag) > 4) {  //redraw down arrow
	    //lower_index1++; upper_index1++;
	    buf2[1] = 0x00; buf2[0] = 0x19;
	    Show_text(14, 144, buf2);
	  }
	  if (lower_index1 > 0) {  //redraw up arrow
	    buf2[1] = 0x00; buf2[0] = 0x18;
	    Show_text(14, 80, buf2);
	  }

	  if (lower_index1 >= 0) {
	    for (i = 5, j = lower_index1; i < (5+upper_index1-lower_index1+1); i++, j++) {
 		   if (j==0)
 	         sprintf(buf2, "%s", Measure_disp_data[5].content);
		   else
              sprintf(buf2, "%s%d", Measure_disp_data[9].content, j);

		   Show_text1(Measure_disp_data[i].disp_x, Measure_disp_data[i].disp_y, "             ");
		   Show_text1(Measure_disp_data[i].disp_x, Measure_disp_data[i].disp_y, buf2);
	    }
	  }
	  LCD_Set_Rect(Measure_disp_data[Region_index].disp_x, Measure_disp_data[Region_index].disp_y, 8*(active_length), 16, 0x00, 2);
	  redraw_screen = 0;
	  continue;
	}

	key = getchar();
    if (key==Up_key || key==Down_key || key==Enter_key)
//	  LCD_Set_Rect(Measure_disp_data[Region_index].disp_x, Measure_disp_data[Region_index].disp_y, 8*(strlen(Measure_disp_data[Region_index].content)), 16, 0xff, 2);
	  LCD_Set_Rect(Measure_disp_data[Region_index].disp_x, Measure_disp_data[Region_index].disp_y, 8*(14), 16, 0xff, 2);
	else
	   continue;

	if (key==Up_key) {
	  Region_index--;
	  if (Region_index < lower_index) {
	    Region_index = lower_index;
	  }
	  else
		 if (Region_index==4) {
		   if (lower_index1 > 0) {
			 lower_index1--; upper_index1--;Region_index++;
			 for (i = 5, j = lower_index1; i < 10; i++, j++) {
				if (j==0) {
				  buf2[1] = 0x00; buf2[0] = 0x20;
			      Show_text1(14, 80, buf2);
			      sprintf(buf2, "%s", Measure_disp_data[5].content);
				}
			    else
                   sprintf(buf2, "%s%d", Measure_disp_data[9].content, j);

				Show_text1(Measure_disp_data[i].disp_x, Measure_disp_data[i].disp_y, "             ");
				if (i==5) {
				    Show_text(Measure_disp_data[i].disp_x, Measure_disp_data[i].disp_y, buf2);	
				}
				else {
			       Show_text1(Measure_disp_data[i].disp_x, Measure_disp_data[i].disp_y, buf2);
				}
			 }
		   }
		   else {
		      Region_index--;
		   }
		 }
	}
	else
	   if (key==Down_key) {
	     Region_index++;
	     if (Region_index > upper_index) {
	       Region_index = upper_index;
		   if (upper_index==9) {
			 if (upper_index1 < (temp_mea1.sample_index+temp_mea1.blank_flag)) {
			   lower_index1++; upper_index1++;
			   buf2[1] = 0x00; buf2[0] = 0x18;
			   Show_text(14, 80, buf2);
			   for (i = 5, j = lower_index1; i < 10; i++, j++) {
			      sprintf(buf2, "%s%d", Measure_disp_data[9].content, j);
				  Show_text1(Measure_disp_data[i].disp_x, Measure_disp_data[i].disp_y, "             ");
				  if (i==9) {
				    Show_text(Measure_disp_data[i].disp_x, Measure_disp_data[i].disp_y, buf2);	
				  }
				  else {
			         Show_text1(Measure_disp_data[i].disp_x, Measure_disp_data[i].disp_y, buf2);
				  }
			   }
			 }
		   }
	     }
		 else
			if (Region_index==4)
			  Region_index++;   
	   }
	switch(Region_index) {
      case 1:
	  case 3:
         strcpy(buf2, "==============");
		 Show_text1(16, 0, buf2)
		 strcpy(buf2, "Desc.");
         Show_text1(16+(15 - strlen(buf2))/2, 0, buf2)
	     break;
	  case 5:
         strcpy(buf2, "==============");
		 Show_text1(16, 0, buf2)
		 strcpy(buf2, "Output");
         Show_text1(16+(15 - strlen(buf2))/2, 0, buf2)
		 break;
	}
	switch(Region_index) {
	  case 1:
	     LCD_Set_Rect(16, 16, 8*(29-16+1), 144, 0x00, 1);
         j = Region_Show_text("Press", 16, 29, 16, 80);
		 Show_img(&temp_disp_data, (char*)gImage_enter_key, 32 << 16 | (sizeof(gImage_enter_key)*8)/32, 23, 16, 0, 1, 1);
		 j = Region_Show_text("to measure blank!", 16, 29, 48, 80);
		 //LCD_Display_Object(&Measure_disp_data[5], 0);
		 //(char*)gImage_enter_key, 32 << 16 | (sizeof(gImage_enter_key)*8)/32, 16, 128, 0, 1
		 //temp_disp_data = { (char*)gImage_enter_key, 32 << 16 | (sizeof(gImage_enter_key)*8)/32, 16, 128, 0, 1 };
		 Show_img(&temp_disp_data, (char*)gImage_logo_blank1, 48 << 16 | (sizeof(gImage_logo_blank1)*8)/48, 18, 112, 0, 1, 1);
		 //temp_disp_data.content = buf2;
	     break;
	  case 2:
		 LCD_Set_Rect(16, 16, 8*(29-16+1), 144, 0x00, 1);
		 j = Region_Show_text("Press", 16, 29, 16, 80);
         Show_img(&temp_disp_data, (char*)gImage_enter_key, 32 << 16 | (sizeof(gImage_enter_key)*8)/32, 23, 16, 0, 1, 1);
		 j = Region_Show_text("to measure samples!", 16, 29, 48, 80);
		 Show_img(&temp_disp_data, (char*)gImage_logo_sample1, 48 << 16 | (sizeof(gImage_logo_sample1)*8)/48, 18, 112, 0, 1, 1);
	     break;
	  case 3:
	     LCD_Set_Rect(16, 16, 8*(29-16+1), 144, 0x00, 1);
	     j = Region_Show_text("Press", 16, 29, 16, 80);
		 Show_img(&temp_disp_data, (char*)gImage_enter_key, 32 << 16 | (sizeof(gImage_enter_key)*8)/32, 23, 16, 0, 1, 1);
	     j = Region_Show_text("to save the sheet & back to main menu!", 16, 29, 48, 96);
		 Show_img(&temp_disp_data, (char*)gImage_save, 48 << 16 | (sizeof(gImage_save)*8)/48, 18, 112, 0, 1, 1);
	     break;
	}
	if (Region_index <= 3) {
	  active_length = strlen(Measure_disp_data[Region_index].content);
	  LCD_Set_Rect(Measure_disp_data[Region_index].disp_x, Measure_disp_data[Region_index].disp_y, 8*(active_length), 16, 0x00, 2);
	}
	else {
	   //Region_index - 5
	   //if (lower_index1 > 0) {
	    if ((upper_index1 - (9-Region_index)) > 0) {
		   sprintf(buf2, "%s%d", Measure_disp_data[9].content, upper_index1 - (9-Region_index));
		   active_length = strlen(buf2);
           LCD_Set_Rect(Measure_disp_data[Region_index].disp_x, Measure_disp_data[Region_index].disp_y, 8*(active_length), 16, 0x00, 2);
		   LCD_Set_Rect(16, 16, 8*(29-16+1), 144, 0x00, 1);
		   Display_measure_output(&temp_mea1, mode_index+1);
	    }
		else
		   if (Region_index==5) {
		     active_length = strlen(Measure_disp_data[Region_index].content);
		     LCD_Set_Rect(Measure_disp_data[Region_index].disp_x, Measure_disp_data[Region_index].disp_y, 8*(active_length), 16, 0x00, 2);
			 LCD_Set_Rect(16, 16, 8*(29-16+1), 144, 0x00, 1);
		   }
		   else {
		      active_length = strlen(Measure_disp_data[Region_index].content)+1;
		      LCD_Set_Rect(Measure_disp_data[Region_index].disp_x, Measure_disp_data[Region_index].disp_y, 8*(active_length), 16, 0x00, 2);
			  LCD_Set_Rect(16, 16, 8*(29-16+1), 144, 0x00, 1);
			  Display_measure_output(&temp_mea1, mode_index+1);
		   }
	}

	if (key==Enter_key) {
	  measure_index = Region_index - 1;
	  switch(Region_index) {
        case 1:
		   err_no = Add_measurement(&temp_mea1, 0);
		   if (!err_no) {
		     LCD_Display_Object(&Measure_disp_data[5], 0);
			 upper_index += 2;
			 lower_index1 = upper_index1 = 0;
		   }
/*		   if (lower_index1==-1) {
		     lower_index1 = upper_index1 = 5;
			 for (i = lower_index1; i < (upper_index1+1); i++)
                LCD_Display_Object(&Measure_disp_data[i], 0);
		   }*/
		   LCD_Set_Rect(0, 0, 240, 160, 0x00, 1);
		   LCD_Set_Rect(0, 0, 240, 160, 0xff, 2);
		   Show_img(&temp_disp_data, (char*)gImage_info, 40 << 16 | (sizeof(gImage_info)*8)/40, 0, 32, 0, 1, 1);
		   sprintf(buf2, "blank: finishing measurement!");
		   j = Region_Show_text(buf2, 7, 29, 32, 80);
		   Delay100ms(20);
		   redraw_screen = 1;
		   goto Measure_menu;
           break;
        case 2:
		   if (temp_mea1.blank_flag > 0) {
			 err_no = Add_measurement(&temp_mea1, 1);
			 i = temp_mea1.sample_index+temp_mea1.blank_flag;
			 if (!err_no && i > 0 && i <= 4) {
			   strcpy(buf2, Measure_disp_data[5+i]);
			   sprintf(buf2, "%s%d", Measure_disp_data[5+i].content, i);
			   Show_text1(Measure_disp_data[5+i].disp_x, Measure_disp_data[5+i].disp_y, buf2);
			   upper_index++;
			   upper_index1++;
//			   LCD_Display_Object(&Measure_disp_data[upper_index1], 0);
			 }
			 else
				 if (i > 4) {
				   buf2[1] = 0x00; buf2[0] = 0x19;
				   Show_text(14, 144, buf2);
				 }
		     LCD_Set_Rect(0, 0, 240, 160, 0x00, 1);
		     LCD_Set_Rect(0, 0, 240, 160, 0xff, 2);
			 Show_img(&temp_disp_data, (char*)gImage_info, 40 << 16 | (sizeof(gImage_info)*8)/40, 0, 32, 0, 1, 1);
			 sprintf(buf2, "%s%d: finishing measurement!", Measure_disp_data[9].content, i);
			 j = Region_Show_text(buf2, 7, 29, 32, 80);
		     Delay100ms(20);
			 redraw_screen = 1;
		     goto Measure_menu;
		   }
		   else {
		      LCD_Set_Rect(0, 0, 240, 160, 0x00, 1);
		      LCD_Set_Rect(0, 0, 240, 160, 0xff, 2);
			  Show_img(&temp_disp_data, (char*)gImage_info, 40 << 16 | (sizeof(gImage_info)*8)/40, 0, 32, 0, 1, 1);
			  j = Region_Show_text("Warning: please measure blank first!", 7, 29, 32, 80);
		      Delay100ms(200);
              printf("Warning: please measure blank first!\n");
			  redraw_screen = 1;
		      goto Measure_menu;
		   }

/*           if (upper_index1 > 0) {
			 if ((upper_index1+1) < 10) {
			   upper_index1 =  upper_index1++;
			   LCD_Display_Object(&Measure_disp_data[upper_index1], 0);
			 }
		   }
		   else {
			  printf("Warning: please measure blank first!\n");
		   }*/

         

           break;
        case 3:
		   LCD_Set_Rect(0, 0, 240, 160, 0x00, 1);
		   LCD_Set_Rect(0, 0, 240, 160, 0xff, 2);
/*		   j = 0;
		   for (i = 0; i < 4; i++) {
		      if (j >= Measure_disp_msg[i].disp_y)
			    row = j;
		      j = Region_Show_text(Measure_disp_msg[i].content, Measure_disp_msg[i].disp_x, 29, row, 144);
		   }
		   j += Region_Show_text(Measure_disp_msg[0].content, Measure_disp_msg[0].disp_x, 29, (j >= Measure_disp_msg[0].disp_y) ? j:Measure_disp_msg[0].disp_y, 144);
           j += Region_Show_text(Measure_disp_msg[1].content, Measure_disp_msg[1].disp_x, 29, (j >= Measure_disp_msg[1].disp_y) ? j:Measure_disp_msg[1].disp_y, 144);
		   j += Region_Show_text(Measure_disp_msg[2].content, Measure_disp_msg[2].disp_x, 29, (j >= Measure_disp_msg[2].disp_y) ? j:Measure_disp_msg[2].disp_y, 144);
		   j += Region_Show_text(Measure_disp_msg[3].content, Measure_disp_msg[3].disp_x, 29, (j >= Measure_disp_msg[3].disp_y) ? j:Measure_disp_msg[3].disp_y, 144);*/
		   for (i = 0; i < 5; i++) {
			  if (i==0) {
			    j = Region_Show_text(Measure_disp_msg[i].content, Measure_disp_msg[i].disp_x, 29, Measure_disp_msg[i].disp_y, 144);
			  }
			  else
                 LCD_Display_Object(&Measure_disp_msg[i], 0);
		   }
	       LCD_Set_Rect(Measure_disp_msg[2].disp_x+4, Measure_disp_msg[2].disp_y, 8*(strlen(Measure_disp_msg[2].content)-4), 16, 0x00, 2);
		   is_save = 0;
		   while ((key1 = getchar())) {
			 LCD_Set_Rect(Measure_disp_msg[is_save+2].disp_x+4, Measure_disp_msg[is_save+2].disp_y, 8*(strlen(Measure_disp_msg[is_save+2].content)-4), 16, 0xff, 2);
			 switch (key1) {
		       case Enter_key:
				  if (is_save==1) {
                    return;
				  }
				  else
					 if (is_save==0) {
					   // store measure data into flash
					   Store_sheet(&temp_mea1, mode_index+1, sheet_info+cur_sheet);
					   i = temp_mea1.sample_index+temp_mea1.blank_flag;
					   if (i > 0) {
						 if (head_sheet==-1) {
                           head_sheet = 0;
						   cur_sheet = tail_sheet = 1;
						 }
						 else {
						    tail_sheet++;
							if (head_sheet < tail_sheet) {
							  cur_sheet = tail_sheet;
							  if (tail_sheet==MAX_Sheets) {
							  	head_sheet++;
                                cur_sheet = tail_sheet = 0;
							  }
							}
							else
							   if (head_sheet==tail_sheet) {
							     cur_sheet = tail_sheet;
							     head_sheet++;
								 if (tail_sheet==MAX_Sheets) {
								   head_sheet = 1;
                                   cur_sheet = tail_sheet = 0;
								 }
							   }
						 }
					   }
					   return;
					 }
					 else {
					    redraw_screen = 1;
		                goto Measure_menu;
					 }
		          break;
			   case Up_key:
			      if (is_save > 0)
			        is_save--;
			      break;
			   case Down_key:
			      if (is_save < 2)
			        is_save++;
			      break;
			 }
			 LCD_Set_Rect(Measure_disp_msg[is_save+2].disp_x+4, Measure_disp_msg[is_save+2].disp_y, 8*(strlen(Measure_disp_msg[is_save+2].content)-4), 16, 0x00, 2);
				  //LCD_Set_Rect(Measure_disp_msg[3].disp_x+4, Measure_disp_msg[3].disp_y, 8*(strlen(Measure_disp_msg[3].content)-4), 16, 0xff, 2);
				  //LCD_Set_Rect(Measure_disp_msg[4].disp_x+4, Measure_disp_msg[4].disp_y, 8*(strlen(Measure_disp_msg[4].content)-4), 16, 0xff, 2);
		   }
           break;
	  }
	}
  }
}

//Page4:  main menu page
void Display_page4()
{
  int i, row = 16, j, col = 0, total_pixels, width, height, key, Region_index = 2, lower_index = 2, upper_index = 5;
  char *pstr;


  screen_inverse 
  LCD_Set(0xff, 2);
  LCD_Set(0x00, 1);
  LCD_Text();

Main_menu:
  for (i = 0; i < sizeof(MM_disp_data) / sizeof(struct STN_data); i++)
     LCD_Display_Object(&MM_disp_data[i], 0);

  if (mode_index < 0) {
    mode_index = 0;
  }
  Show_text(30 - strlen(disp_data[mode_index+2].content+4), 0, disp_data[mode_index+2].content+4)
  if (menu_index >= 0) {
    Region_index = menu_index + 2;
  }

  LCD_Set_Rect(MM_disp_data[Region_index].disp_x+4, MM_disp_data[Region_index].disp_y, 8*(strlen(MM_disp_data[Region_index].content)-4), 16, 0x00, 2);
  //LCD_Display_Object(&MM_disp_data[Region_index], 1);
  LCD_Display_Object(&icon_data[icon_up_key], 1);
  LCD_Display_Object(&icon_data[icon_down_key], 1);
  LCD_Display_Object(&icon_data[icon_enter_key], 1);

  /*LCD_GotoXY(30 - strlen(disp_data[mode_index+2].content+4), 0);
  LCD_ShowStr(disp_data[mode_index+2].content+4, 0);
  LCD_Set_Rect(30 - strlen(disp_data[mode_index+2].content+4), 0, 8*strlen(disp_data[mode_index+2].content+4), 16, 0x00, 2);*/
  while ((key = getchar()) != Enter_key) {
	switch (key) {
      case Up_key:
         LCD_Display_Object(&icon_data[icon_up_key], 0);
  	     LCD_Display_Object(&icon_data[icon_down_key], 1);
	     break;
	  case Down_key:
	     LCD_Display_Object(&icon_data[icon_up_key], 1);
	     LCD_Display_Object(&icon_data[icon_down_key], 0);
	     break;
	}

    LCD_Set_Rect(MM_disp_data[Region_index].disp_x, disp_data[Region_index].disp_y, 8*strlen(MM_disp_data[Region_index].content), 16, 0xff, 2);

	if (key==Up_key) {
	  Region_index--;
	  if (Region_index < lower_index) {
	    Region_index = upper_index;
	  }
	}
	else
	   if (key==Down_key) {
	     Region_index++;
	     if (Region_index > upper_index) {
	       Region_index = lower_index;
	     }
	   }
	   else
          if (key==Enter_key) {
		  }

	/*switch (Region_index) {
	  case 2:
	     LCD_Display_Object(&icon_data[icon_dsDNA], 1);
		 break;
	  case 3:
	     LCD_Display_Object(&icon_data[icon_ssDNA], 1);
	     break;
	  case 4:
		 LCD_Display_Object(&icon_data[icon_RNA], 1);
	     break;
	  case 5:
		 LCD_Display_Object(&icon_data[icon_protein], 1);
	     break;
	}*/
    //LCD_Text();
    //Access_Page2();
	LCD_Set_Rect(MM_disp_data[Region_index].disp_x+4, MM_disp_data[Region_index].disp_y, 8*(strlen(MM_disp_data[Region_index].content)-4), 16, 0x00, 2);
    Delay100ms(5);
//	last_key = key;
  }
  menu_index = Region_index - 2;
  return;
while(1);

  //  LCD_CmdWrite(WLCR);
  //  LCD_DataWrite(0x04); //05

  Gray_Mode();
  LCD_Set(0xff, 0);
  //  LCD_Clear();
  //  Access_Page1();

  LCD_GotoXY(8, 0);
  LCD_CmdWrite(MWCR);
  // for (i = 0; i < sizeof(gImage_logo); i++)
  //   LCD_DataWrite(~gImage_logo[i]);

  Delay100ms(20);
  /*for (i = 0; i < sizeof(gImage_logo_blank); i++) {
  if ((i % 120)==0) {
  row++;
  LCD_GotoXY(0, row);
  LCD_CmdWrite(MWCR);
  printf("row=%d\n", row);
  }
  LCD_DataWrite(~gImage_logo_blank[i]);
  }*/

  //for (i = 0; i < (240*160)/16; i++)
  //    LCD_DataWrite(0x0f); 

  /*for (j = 0; j < sizeof(page4_disp_data) / sizeof(struct STN_data); j++)
  {
    col = page4_disp_data[j].disp_x;
    row = page4_disp_data[j].disp_y;
    for (i = 0; i < sizeof(gImage_logo_blank); i++)
    {
      if ((i % 12) == 0)
      {
        LCD_GotoXY(col, row);
        LCD_CmdWrite(0xB0);
        row++;
      } LCD_DataWrite(~gImage_logo_blank[i]);
    }
  }*/

  i = 0;
  while (i < 10)
  {
    i %= 4;
    if (!i)
    {
      LCD_GotoXY(page4_disp_data[3].disp_x - 1, page4_disp_data[3].disp_y - 5);
      row = page4_disp_data[3].disp_y - 5;
      col = page4_disp_data[3].disp_x - 1;
      pstr = page4_disp_data[3].content;
    }
    else
    {
      LCD_GotoXY(page4_disp_data[i - 1].disp_x - 1, page4_disp_data[i -1].disp_y - 5);
      row = page4_disp_data[i - 1].disp_y - 5;
      col = page4_disp_data[i - 1].disp_x - 1;
      pstr = page4_disp_data[i - 1].content;
    }

    width = 16+48;
    height = 10+48;
    total_pixels = (width *height) / 4;
    for (j = 0; j < total_pixels; j++)
    {
      if ((j % 16) == 0)
      {
        LCD_GotoXY(col, row);
        LCD_CmdWrite(0xB0);
        row++;
      }
      LCD_DataWrite(0xff);
    }

    if (!i)
    {
      col = page4_disp_data[3].disp_x;
      row = page4_disp_data[3].disp_y;
    }
    else
    {
      col = page4_disp_data[i - 1].disp_x;
      row = page4_disp_data[i - 1].disp_y;
    }
    for (j = 0; j < sizeof(gImage_logo_blank); j++)
    {
      if ((j % 12) == 0)
      {
        LCD_GotoXY(col, row);
        LCD_CmdWrite(0xB0);
        row++;
      }
      LCD_DataWrite(~pstr[j]);
    }

    col = page4_disp_data[i].disp_x - 1;
    row = page4_disp_data[i].disp_y - 5;
    width = 16+48;
    height = 10+48;
    total_pixels = (width *height) / 4;
    for (j = 0; j < total_pixels; j++)
    {
      if ((j % 16) == 0)
      {
        LCD_GotoXY(col, row);
        LCD_CmdWrite(0xB0);
        row++;
      }
      LCD_DataWrite(0x00);
    }
    col = page4_disp_data[i].disp_x;
    row = page4_disp_data[i].disp_y;
    pstr = page4_disp_data[i].content;
    for (j = 0; j < sizeof(gImage_logo_blank); j++)
    {
      if ((j % 12) == 0)
      {
        LCD_GotoXY(col, row);
        LCD_CmdWrite(0xB0);
        row++;
      }
      LCD_DataWrite(pstr[j]);
    }
    i++;
    Delay100ms(20);
  }
  /*LCD_GotoXY(19, 0);
  row = 16;
  for (i = 0; i < sizeof(gImage_logo_blank); i++)  {
  if ((i % 12)==0) {
  LCD_GotoXY(19, row);
  LCD_CmdWrite(0xB0);
  row++;
  }
  LCD_DataWrite(~gImage_logo_blank[i]);
  }


  LCD_GotoXY(4, 0);
  row = 16 + 48 + 32;
  for (i = 0; i < sizeof(gImage_logo_blank); i++)  {
  if ((i % 12)==0) {
  LCD_GotoXY(4, row);
  LCD_CmdWrite(0xB0);
  row++;
  }
  LCD_DataWrite(~gImage_logo_blank[i]);
  }

  LCD_GotoXY(19, 0);
  row = 16 + 48 + 32;
  for (i = 0; i < sizeof(gImage_logo_blank); i++)  {
  if ((i % 12)==0) {
  LCD_GotoXY(19, row);
  LCD_CmdWrite(0xB0);
  row++;
  }
  LCD_DataWrite(~gImage_logo_blank[i]);
  }*/
}

//Page1: mode select
void Display_page1()
{
  int i, j, key, up_down = -1, Region_index = 2, lower_index = 2, upper_index=5, last_key = -1, comfirm_yes_no;
  char buf[10];

  screen_inverse 
  //  LCD_Clear();
  //  Only_Show_Page2();
  //  LCD_Clear();
  //LCD_Set(0x00, 1);
  LCD_Set(0xff, 2);
  Only_Show_Page2();
  LCD_Set(0x00, 1);
  //XOR_Two_Layer();
  //LCD_CmdWrite(WLCR); //[00H] , Default --> 0x00
  //LCD_DataWrite(0x05); //05
  //  Show_Two_Layer();
  LCD_Text();

  //Access_2Page();
  //LCD_FontSize(5);
  //strcpy(buf2, "Select:  ");
  //disp_data[5].content[7] = 0x18;
  //disp_data[5].content[8] = 0x19;
  ///disp_data[5].disp_x = 10;
  //printf("kkk: %d", disp_data[5].content[8]);
Select_measure_sample:
  for (i = 0; i < sizeof(disp_data) / sizeof(struct STN_data); i++)
  {
    /*LCD_GotoXY(disp_data[i].disp_x, disp_data[i].disp_y);
    if (disp_data[i].layer == 0)
    {
      Access_Page1();
    } 
    else
    if (disp_data[i].layer == 1)
    {
      Access_Page2();
    }
    else
    if (disp_data[i].layer == 2)
    {
      Access_2Page();
    }*/
    if ((i + 1) >= sizeof(disp_data) / sizeof(struct STN_data))
    {
      LCD_GotoXY(disp_data[i].disp_x, disp_data[i].disp_y);
	  LCD_FontSize(5);
      disp_data[i].content[0] = 0x19;
      strcpy(disp_data[i].content+1, "       ");
      disp_data[i].content[8] = 0x18;
      disp_data[i].content[9] = 0x00;
//      LCD_ShowStr(disp_data[i].content, strlen(disp_data[i].content));
    } 
    else
    {
      //LCD_FontSize(0);
//      LCD_ShowStr(disp_data[i].content, strlen(disp_data[i].content));
//	  LCD_GotoXY(disp_data[i].disp_x+15, disp_data[i].disp_y);
//	  LCD_ShowStr(disp_data[i].content, strlen(disp_data[i].content));
      LCD_Display_Object(&disp_data[i], 0);
    }
    //LCD_ShowStr(disp_data[i].content, strlen(disp_data[i].content));

    /*	  LCD_GotoXY(disp_data[i].disp_x, disp_data[i].disp_y);
    if (disp_data[i].layer==0) {
    Access_Page2();
    }
    else
    if (disp_data[i].layer==1) {
    Access_Page1();
    }
    LCD_ShowStr(" ", strlen(disp_data[i].content));*/
  }

  /*for (i = 0; i < 53; i++) {
  LCD_GotoXY(80, (i)*16);
  if (i > 30)
  LCD_ShowStr("b",1);
  else
  LCD_ShowStr("a",1);
  Delay100ms(10);
  }	 */
    //Only_Show_Page1();
    //Only_Show_Page2();
  XOR_Two_Layer();
  Show_Two_Layer();
  LCD_FontSize(0);
  //while(1);

  /*LCD_Graphic();
  LCD_GotoXY(0, 0);
  LCD_CmdWrite(MWCR);
  for (i = 0; i < 1800; i++)
  LCD_DataWrite(~gImage_TopHeadline_24060[i]);		*/
  //while(1) ;

  i = 1;
  key = 0;
  LCD_FontSize(0);
    
/*	Access_Page2();
    LCD_GotoXY(disp_data[1].disp_x, disp_data[1].disp_y);
    j = strlen(disp_data[1].content);
    buf[0] = 0x20;
    buf[1] = '\0';

    while (j-- > 0)
      LCD_ShowStr(buf, 1);*/
  Region_index = 2;
//  LCD_GotoXY(30 - strlen(disp_data[2].content+4), 0); \
//  LCD_ShowStr(disp_data[2].content+4, 0); \
//  Only_Show_Page1();
//  LCD_Set_Rect(30 - strlen(disp_data[2].content+4), 0, 8*strlen(disp_data[2].content+4), 16, 0x00, 2);
//  Only_Show_Page2();
//  LCD_Display_Object(&disp_data[Region_index], 1);
  if (mode_index >= 0) {
    LCD_Set_Rect(disp_data[mode_index+Region_index].disp_x, disp_data[mode_index+Region_index].disp_y, 8*(strlen(disp_data[mode_index+Region_index].content)), 16, 0x00, 2);  
	Show_text(30 - strlen(disp_data[2+mode_index].content+4), 0, disp_data[2+mode_index].content+4)
    Region_index = 2+mode_index;
  }
  else {
     LCD_Set_Rect(disp_data[Region_index].disp_x, disp_data[Region_index].disp_y, 8*(strlen(disp_data[Region_index].content)), 16, 0x00, 2);  
	 Show_text(30 - strlen(disp_data[2].content+4), 0, disp_data[2].content+4)
  }

  switch (Region_index) {
    case 2:
       LCD_Display_Object(&icon_data[icon_dsDNA], 1);
       break;
    case 3:
       LCD_Display_Object(&icon_data[icon_ssDNA], 1);
       break;
    case 4:
       LCD_Display_Object(&icon_data[icon_RNA], 1);
       break;
    case 5:
       LCD_Display_Object(&icon_data[icon_protein], 1);
       break;
  }
  LCD_Display_Object(&icon_data[icon_up_key], 1);
  LCD_Display_Object(&icon_data[icon_down_key], 1);
  LCD_Display_Object(&icon_data[icon_enter_key], 1);

#ifdef Keyboard_Emu
  while ((key = getchar()) != Enter_key)
//  while (key = getchar())
#else
  while (1)
#endif
  {
	printf("key code: %ld\n", key);
	if (key==Up_key)
	   up_down = -1;
	else
	    if (key==Down_key)
		  up_down = 1;
//    else
//	    continue;

	if (up_down==-1) {
//	  LCD_GotoXY(5, 128);
//	  LCD_Set_Rect(3, 128, 16, 32, 0xff, 2);
//  	  LCD_Set_Rect(19, 128, 16, 32, 0, 2);
	}
	else {
//	    LCD_GotoXY(15, 128);
//	    LCD_Set_Rect(3, 128, 16, 32, 0, 2);
//  	    LCD_Set_Rect(19, 128, 16, 32, 0xff, 2);
	}
    //cur_index = 1;
/*	switch(last_key) {
	  case Up_key:
	     LCD_Display_Object(&icon_data[4], 1);
	     break;
	  case Down_key:
	     LCD_Display_Object(&icon_data[5], 1);
	     break;
	  case Enter_key:
         LCD_Display_Object(&icon_data[6], 1);
		 break;
	}*/

	switch(key) {
	  case Up_key:
	     LCD_Display_Object(&icon_data[icon_up_key], 0);
		 LCD_Display_Object(&icon_data[icon_down_key], 1);
	     break;
	  case Down_key:
		 LCD_Display_Object(&icon_data[icon_up_key], 1);
	     LCD_Display_Object(&icon_data[icon_down_key], 0);
	     break;
	  case Enter_key:
         LCD_Display_Object(&icon_data[icon_enter_key], 0);
		 break;
	}

    //LCD_Display_Object(&disp_data[Region_index], 1);
    LCD_Set_Rect(icon_data[icon_dsDNA].disp_x, icon_data[icon_dsDNA].disp_y, 80, 80, 0x00, 1);
    LCD_Set_Rect(disp_data[Region_index].disp_x, disp_data[Region_index].disp_y, 8*strlen(disp_data[Region_index].content), 16, 0xff, 2);
    //LCD_Set(0xff, 2);

    /*buf[0] = 0xff;
    if (i > 0) {
    LCD_GotoXY(disp_data[i-1].disp_x, disp_data[i-1].disp_y);
    j = strlen(disp_data[i-1].content);
    }
    else {
    LCD_GotoXY(disp_data[3].disp_x, disp_data[3].disp_y);
    j = strlen(disp_data[3].content);	
    }
    while (j-- > 0)
    LCD_ShowStr(buf, 1);*/	

    /*if (k==Up_key) {
	  i--;
	  i %= 5;
      if (i == 0)
	     i = 4;
	}
	else
	    if (k==Down_key) {
		  i++;
	      i %= 5;
          if (i == 0)
	        i = 1;
		}*/
	if (key==Up_key) {
	  Region_index--;
	  if (Region_index < lower_index) {
	    Region_index = upper_index;
	  }
	}
	else
	   if (key==Down_key) {
	     Region_index++;
	     if (Region_index > upper_index) {
	       Region_index = lower_index;
	     }
	   }
	   else
          if (key==Enter_key) {
		  }

    /*LCD_GotoXY(disp_data[i].disp_x, disp_data[i].disp_y);
    j = strlen(disp_data[i].content);
    buf[0] = 0x20;
    buf[1] = '\0';

    while (j-- > 0)
      LCD_ShowStr(buf, 1);*/
	switch (Region_index) {
	  case 2:
	     LCD_Display_Object(&icon_data[icon_dsDNA], 1);
		 break;
	  case 3:
	     LCD_Display_Object(&icon_data[icon_ssDNA], 1);
	     break;
	  case 4:
		 LCD_Display_Object(&icon_data[icon_RNA], 1);
	     break;
	  case 5:
		 LCD_Display_Object(&icon_data[icon_protein], 1);
	     break;
	}
    //LCD_Text();
    //Access_Page2();
	LCD_Set_Rect(disp_data[Region_index].disp_x, disp_data[Region_index].disp_y, 8*strlen(disp_data[Region_index].content), 16, 0x00, 2);
    Delay100ms(5);
	last_key = key;
    //k++;
  }
  
//#ifdef 1
  LCD_Display_Object(&icon_data[icon_enter_key], 0);
  LCD_Display_Object(&icon_data[icon_up_key], 1);
  LCD_Display_Object(&icon_data[icon_down_key], 1);
  LCD_Set_Rect(disp_data[Region_index].disp_x, disp_data[Region_index].disp_y, 8*strlen(disp_data[Region_index].content), 16, 0xff, 2);
  LCD_Set_Rect(disp_data[lower_index].disp_x, disp_data[lower_index].disp_y, 240, disp_data[upper_index].disp_y-disp_data[lower_index].disp_y+32, 0x00, 1);
  //Do you want to Enter this mode
  strcpy(buf2, "Do you want to measure sample: ");
  strcat(buf2, disp_data[Region_index].content+4);
  disp_data[sizeof(disp_data) / sizeof(struct STN_data)-1].disp_x = disp_data[lower_index].disp_x;
  disp_data[sizeof(disp_data) / sizeof(struct STN_data)-1].disp_y = disp_data[lower_index].disp_y;
  LCD_Display_Object(&disp_data[sizeof(disp_data) / sizeof(struct STN_data)-1], 0);
  LCD_Display_Object(&icon_data[icon_yes], 1);
  LCD_Display_Object(&icon_data[icon_no], 0);
  comfirm_yes_no = 1;

  while ((key = getchar()) != Enter_key) {
	switch(key) {
	  case Up_key:
//	     LCD_Display_Object(&icon_data[7], 0);
	     LCD_Display_Object(&icon_data[icon_up_key], 0);
		 LCD_Display_Object(&icon_data[icon_down_key], 1);
		 LCD_Display_Object(&icon_data[icon_enter_key], 1);
	     break;
	  case Down_key:
//	     LCD_Display_Object(&icon_data[8], 0);
	     LCD_Display_Object(&icon_data[icon_up_key], 1);
		 LCD_Display_Object(&icon_data[icon_down_key], 0);
		 LCD_Display_Object(&icon_data[icon_enter_key], 1);
	     break;
	}

	if (comfirm_yes_no==1) {
	  if (key==Down_key) {
        LCD_Display_Object(&icon_data[icon_yes], 0);
        LCD_Display_Object(&icon_data[icon_no], 1);
        comfirm_yes_no = 0;
	  }
	}
	else 
	  if (key==Up_key) {
        LCD_Display_Object(&icon_data[icon_yes], 1);
        LCD_Display_Object(&icon_data[icon_no], 0);
        comfirm_yes_no = 1;
	  }
  }
//#endif
  if (comfirm_yes_no==0) {
    LCD_Set_Rect(disp_data[lower_index].disp_x, disp_data[lower_index].disp_y, 240, disp_data[upper_index].disp_y-disp_data[lower_index].disp_y+32, 0x00, 1);
    goto Select_measure_sample;
  }
  else {
     mode_index = Region_index - 2;
  }

  /*temp_disp_data.content = buf2;
  temp_disp_data.length = temp_disp_data.layer = temp_disp_data.text_graphic;
  temp_disp_data.disp_x = disp_data[lower_index].disp_x;
  temp_disp_data.disp_y = disp_data[lower_index].disp_y;
  LCD_Display_Object(&temp_disp_data, 0);*/
  /*LCD_Text();
  LCD_GotoXY(disp_data[Region_index].disp_x, disp_data[Region_index].disp_y);
  LCD_ShowStr(buf2, 0);*/
  
  //Only_Show_Page1();
  /*Backup_DRAM(layer1_DRAM1, 0);
  Backup_DRAM(layer2_DRAM2, 1);
  LCD_Clear();
  Delay100ms(100);
  Restore_DRAM(layer1_DRAM1, 0);
  Restore_DRAM(layer2_DRAM2, 1);
  while(1);*/
}

void Display_logo3()
{
  int i, j, cur_y;
  char buf[10];

  /*screen_normal
  LCD_Clear();*/
  screen_inverse
  LCD_Set(0xff, 2);
  LCD_Set(0x00, 1);
  XOR_Two_Layer();
  Show_Two_Layer();

  LCD_Display_Object(&logo_disp_data[0], 0);
  //while(1);

  cur_y = ((20 + sizeof(gImage_logo3))*8/240 /  16 + 2) * 16;
/*  LCD_Text();
  LCD_GotoXY(0, cur_y);
  LCD_ShowStr("http://www.maestrogen.com/", 0);*/
  //LCD_Display_Object(&logo_disp_data[1], 1);
  Show_text((30 - strlen(logo_disp_data[1].content))/2, cur_y, logo_disp_data[1].content)
/*  Show_Two_Layer();
  XOR_Two_Layer();*/
//  Only_Show_Page1();


  //Only_Show_Page2();
  //while(1);

  Delay100ms(200);
/*  LCD_Clear();
  for (j = 20; j >= 0; j--) {
     LCD_GotoXY(j, 20);
     LCD_CmdWrite(MWCR);	 
     for (i = 0; i < sizeof(gImage_nano_logo); i++)
        LCD_DataWrite(gImage_nano_logo[i]);
	 Delay100ms(5);
  }*/
  /*screen_inverse*/

  LCD_Clear();
  LCD_Set(0xff, 2);
  Access_Page_SW(0);

/*  for (j = 0; j < 80; j++) {
     LCD_GotoXY(0, 40+j);
	 LCD_CmdWrite(MWCR);
     for (i = 0; i < 8; i++) {
	    LCD_DataWrite(gImage_nano_logo[30*j+i]);
	 }
  }*/
  Show_partial_img(gImage_nano_logo, 30, 80, 0, 80, 0, 8, 0, 40);
  Delay100ms(20);
/*  for (j = 0; j < 80; j++) {
  	 LCD_GotoXY(8, 40+j);
	 LCD_CmdWrite(MWCR);
     for (i = 8; i < 16; i++) {
	    LCD_DataWrite(gImage_nano_logo[30*j+i]);
	 }
  }*/
  Show_partial_img(gImage_nano_logo, 30, 80, 0, 80, 8, 16, 8, 40);
  Delay100ms(20);
/*  for (j = 0; j < 80; j++) {
  	 LCD_GotoXY(16, 40+j);
	 LCD_CmdWrite(MWCR);
     for (i = 16; i < 24; i++) {
	    LCD_DataWrite(gImage_nano_logo[30*j+i]);
	 }
  }*/
  Show_partial_img(gImage_nano_logo, 30, 80, 0, 80, 16, 23, 16, 40);
  Delay100ms(20);

/*  for (j = 0; j < 80; j++) {
  	 LCD_GotoXY(24, 40+j);
	 LCD_CmdWrite(MWCR);
     for (i = 24; i < 30; i++) {
	    LCD_DataWrite(gImage_nano_logo[30*j+i]);
	 }
  }*/
  Show_partial_img(gImage_nano_logo, 30, 80, 0, 80, 23, 30, 23, 40);  
  Delay100ms(20);

  LCD_Text();
  LCD_GotoXY(3, 15);
  LCD_FontSize(0x05);
  buf[0] = 0x18;
  buf[1] = 0x00;
  LCD_ShowStr(buf, 1);

  LCD_GotoXY(17, 15);
  LCD_ShowStr(buf, 1);

  buf[0] = 0x19;
  buf[1] = 0x00;
  LCD_GotoXY(10, 120);
  LCD_ShowStr(buf, 1);

  LCD_GotoXY(25, 120);
  LCD_ShowStr(buf, 1);

  LCD_FontSize(0x0);
  LCD_GotoXY(2, 0);
  LCD_ShowStr("Allen", 1);
  LCD_GotoXY(16, 0);
  LCD_ShowStr("Jerry", 1);
  LCD_GotoXY(8, 144);
  LCD_ShowStr("Danny", 1);
  LCD_GotoXY(21, 144);
  LCD_ShowStr("michael", 1);
  //cur_y = ((40 + sizeof(gImage_nano_logo))*8/240 /  16 + 2) * 16;
  
  
//  Only_Show_Page2();
//while(1);
  return;
  
  //LCD_ShowStr("Allen & Danny & Jerry & michael", 0);
 /*(0, 0)-(239, 0)-(239,159)-(0,159)
 (1,1)-(239-1,1)-(239-1,159-1)-(1, 159-1)*/
  LCD_Clear();
  LCD_GotoXY(0, 20);
  LCD_CmdWrite(MWCR);
  for (i = 0; i < sizeof(gImage_logo3); i++)
    LCD_DataWrite(~gImage_yes_no_msg[i]);
}


/******************************************************************************/
/*Main program area		  													  */
/******************************************************************************/
int main(void)
{
  //char  *buf1="select:  ";
  char buf1[10];
  extern uchar Key_Code1, Key_Code2, Key_Code3, Key_Num;
  int i = 0;
  uchar temp1 = 1;
  strcpy(buf1, "Select:  ");
  buf1[strlen(buf1)] = 0x18;
  //buf1[8] = 0x19;



  /*	P0 = 0xff;
  P1 = 0xff;
  P2 = 0xff;
  #ifdef Parallel_8080
  P3 = 0xff;
  #endif

  #ifdef Parallel_6800
  P3 = 0x77;
  #endif

  #ifdef Parallel_8080_4bit
  P3 = 0xff;
  #endif

  #ifdef Real_8080
  P3 = 0xff;
  #endif


  AUXR = B0000_0011;	//Enable MOVX for external interface*/
  Init_console();
  Init_Interface_IO();
  LCD_Reset();
  LCD_Initial();

  LCD_CmdWrite(BTMR);
  LCD_DataWrite(0x20);

  LCD_CmdWrite(ITCR);
  //LCD_DataWrite(595); //75Hz
  LCD_DataWrite(470); //75Hz

/*  PWM control backlight brightness */
  LCD_CmdWrite(PCR); //D0h
  LCD_DataWrite(0x80);
  LCD_CmdWrite(PDCR); //D1h
  LCD_DataWrite(0x50); //0x00



  LCD_ON();
  LCD_Clear();


  Access_Page1();
  Only_Show_Page1();
  screen_rotate_180

    LCD_Clear();

    LCD_Text();
	//LCD_Clear();
	//Gray_Mode();
    LCD_GotoXY(0, 0);
    /*do
    {
      LCD_ShowStr("p", 0);
      i++;
    }
    while (i < 480);
    printf("test ok\n");
	Delay100ms(100);*/
    //screen_normal
	//while(1);

  #ifdef PRETEST
    LCD_Graphic();
    LCD_GotoXY(0, 0);
    LCD_CmdWrite(MWCR);
    for (i = 0; i < 1800; i++)
      LCD_DataWrite(~gImage_TopHeadline_24060[i]);



    LCD_GotoXY(0, 60);
    LCD_CmdWrite(MWCR);
    for (i = 0; i < 3000; i++)
      LCD_DataWrite(~gImage_front_page_ic[i]);

    //i = 0;
    //while (i < 3) {
    Delay100ms(20);
    //  i++;
    //}
    LCD_Clear();

    LCD_Text();
    LCD_GotoXY(0, 0);
    do
    {
      LCD_ShowStr("a", 0);
      i++;
    }
    while (i < 9600);
    printf("test ok\n");


    Delay100ms(20);
    LCD_Clear();
    //LCD_Graphic();
    Gray_Mode();
    LCD_GotoXY(0, 0);
    LCD_CmdWrite(MWCR);
    for (i = 0; i < 9180; i++)
      LCD_DataWrite(~gImage_logo[i]);


    Delay100ms(20);
    LCD_Clear();
    LCD_Graphic();
    LCD_GotoXY(0, 0);
    LCD_CmdWrite(MWCR);
    for (i = 0; i < 9180; i++)
      LCD_DataWrite(~gImage_logo2[i]);

    //  LCD_Clear();
    LCD_Text();

    Show_Two_Layer();
    //  Access_2Page();
    LCD_GotoXY(100, 100);
    //  i = 0;
    //  do
    //  {
    LCD_ShowStr("ABCD", 0);
    //    i++;
    //  }
    //  while (i < 9600);
    //Gray_Mode();
    //  Only_Show_Page1();
  #endif 
  //  LCD_SET(0xff, 2);


  Display_logo3();
  Delay100ms(100);
//  Display_page1();
//  while(1);
  /*while (1) {
    COM_DIR_239_0();
	SEG_DIR_319_0();
	Display_page1();
	Delay100ms(100);
	COM_DIR_0_239();
	SEG_DIR_0_319();
  }*/
    ;
  //Display_page3();
  //while(1);
  while (1) {
    Display_page4();
	switch (menu_index) {
	  case 0:
	     Display_page1();
	     break;
	  case 1:
		 Display_page5();
	//while (1);
		 break;
	  case 2:
	     Display_page6();
		 //while (1);
		 getchar();
	     break;
	  case 3:
	     Display_logo3();
		 Delay100ms(100);
	     break;
	}
  }
  printf("finish test");
  while(1);

  return 0;
  /*{
  for (i=0 ; i<16 ; i++){
  LCD_CmdWrite(PDCR);
  LCD_DataWrite(0x38);//(0x02+i*8);
  Test0_Graphics();
  while (SW==0 && temp1==1){
  while (SWD==0){ 
  Delay10ms(100);
  temp1=0;
  }
  }
  }
  }*/


}
